<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">

          <el-form-item label="物料编号" prop="goodId">
            <el-input
              v-model="queryParams.goodId"
              placeholder="请输入物料编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="状态" prop="sts">
        <el-select v-model="queryParams.sts" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.ck_sts"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
          <el-form-item label="出库单号" prop="orderId">
            <el-input
              v-model="queryParams.orderId"
              placeholder="请输入出库单号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="物料名称" prop="goodName">
            <el-input
              v-model="queryParams.goodName"
              placeholder="请输入物料名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="上架时间" prop="addTimes">
        <el-date-picker
          clearable
          v-model="queryParams.addTimes"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期时间"
          end-placeholder="结束日期时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择上架时间范围">
        </el-date-picker>
      </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

    <el-row :gutter="10" class="mb8">


      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border
              ref="filterTable" v-loading="loading" :data="ORDERDETAILList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column fixed label="出库单号" align="center" prop="orderId"/>
          <el-table-column label="物料编号" align="center" prop="goodId"/>
          <el-table-column label="物料名称" align="center" prop="goodName"/>
          <el-table-column label="库位号" align="center" prop="locationId"/>
          <el-table-column label="托盘号" align="center" prop="ctl"/>
          <el-table-column label="状态" align="center" prop="sts">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.ck_sts" :value="scope.row.sts"/>
            </template>
          </el-table-column>
          <el-table-column label="出库类型"  align="center" sortable prop="type">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.ck_type" :value="scope.row.type"/>
            </template>
          </el-table-column>
          <el-table-column label="数量" align="center" prop="goodsNum"/>
          <el-table-column fixed="right" label="创建时间" align="center" prop="addtime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.addtime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
        </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出库物料明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="${comment}" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="BUSINESSID">
          <el-input v-model="form.BUSINESSID" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="goodId">
          <el-input v-model="form.goodId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="sts">
          <el-input v-model="form.sts" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="SPECIFICATION">
          <el-input v-model="form.SPECIFICATION" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="REMARK">
          <el-input v-model="form.REMARK" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="${comment}" prop="scaleUnit">
          <el-input v-model="form.scaleUnit" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="goodsTypeId">
          <el-input v-model="form.goodsTypeId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="addtime">
          <el-date-picker clearable
            v-model="form.addtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="endtime">
          <el-date-picker clearable
            v-model="form.endtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="UNIT">
          <el-input v-model="form.UNIT" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="storageAreaId">
          <el-input v-model="form.storageAreaId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="locationId">
          <el-input v-model="form.locationId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="storageId">
          <el-input v-model="form.storageId" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listORDERDETAIL, getORDERDETAIL, delORDERDETAIL, addORDERDETAIL, updateORDERDETAIL,listORDERDETAILs } from "@/api/system/ORDERDETAIL";

export default {
  name: "ORDERDETAIL",
  dicts: ['ck_sts', 'kc_sts','ck_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出库物料明细表格数据
      ORDERDETAILList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        addTimes:[],
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        BUSINESSID: null,
        goodId: null,
        goodName: null,
        sts: null,
        SPECIFICATION: null,
        goodsNum: null,
        SORTID: null,
        REMARK: null,
        // pageNum: null,
        WEITH: null,
        PRICE: null,
        scaleUnit: null,
        goodsTypeId: null,
        packingNum: null,
        addtime: null,
        endtime: null,
        totalVolume: null,
        GVOLUME: null,
        UNIT: null,
        storageAreaId: null,
        locationId: null,
        pickNum: null,
        trNum: null,
        xqType: null,
        buyNum: null,
        storageId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    /** 查询出库物料明细列表 */
    getList() {
      this.loading = true;
      listORDERDETAILs(this.queryParams).then(response => {
        this.ORDERDETAILList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        BUSINESSID: null,
        goodId: null,
        goodName: null,
        sts: null,
        SPECIFICATION: null,
        goodsNum: null,
        SORTID: null,
        REMARK: null,
        pageNum: null,
        WEITH: null,
        PRICE: null,
        scaleUnit: null,
        goodsTypeId: null,
        packingNum: null,
        addtime: null,
        endtime: null,
        totalVolume: null,
        GVOLUME: null,
        UNIT: null,
        storageAreaId: null,
        locationId: null,
        pickNum: null,
        trNum: null,
        xqType: null,
        buyNum: null,
        storageId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出库物料明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getORDERDETAIL(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出库物料明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateORDERDETAIL(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addORDERDETAIL(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除出库物料明细编号为"' + orderIds + '"的数据项？').then(function() {
        return delORDERDETAIL(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/ORDERDETAIL/export', {
        ...this.queryParams
      }, `ORDERDETAIL_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
